<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

class AutoRouterImpl implements IRouter
{

  /**
   * 
   * @var array
   */
  protected $parsed = array();

  /**
   * 
   * @var ICache
   */
  protected $cache;

  /**
   * 
   * @var string
   */
  protected $cacheid = 'Simplify/Router/Routes/Auto';

  public function initialize() {}

  protected function getCache()
  {
    static $cache;
    
    if (empty($cache)) {
      $cache = new FileCache(Simplify::getConfig()->get('{env}:cache:dir'));
      
      if ($cache->cached($this->cacheid)) {
        $this->cache = @unserialize($cache->read($this->cacheid));
        
        if (!is_array($this->cache)) {
          $this->cache = array();
        }
      }
    }
    
    return $cache;
  }

  protected function cache($route, $data)
  {
    $cache = $this->getCache();
    $this->cache[$route] = $data;
    $cache->write($this->cacheid, serialize($this->cache), 9999);
  }

  public function build($route, $params)
  {
  }

  public function parse($route)
  {
    $this->getCache();
    
    if (!isset($this->cache[$route])) {
      $base = Simplify::getConfig()->get('{env}:home_dir');
      
      //$route = str_replace(array('-', ' '), '_', $route);
      
      $words = preg_split('#/#', $route, - 1, PREG_SPLIT_NO_EMPTY);
      
      /**
       * find path
       */
      
      $path = '/';
      
      $dir = Simplify::getConfig()->get('{env}:home_controller');
      
      while (count($words) && is_dir($base . $path . str_replace(array('-', ' '), '_', $words[0]))) {
        $dir = str_replace(array('-', ' '), '_', $words[0]);
        $path .= str_replace(array('-', ' '), '_', array_shift($words)) . '/';
      }
      
      /**
       * find controller
       */
      
      if (count($words)) {
        //$file = $base . $path . '/' . $words[0] . '_controller.php';
        $file = $base . $path . str_replace(array('-', ' '), '_', $words[0]) . '_controller.php';
        
        if (file_exists($file)) {
          $controller = str_replace(array('-', ' '), '_', array_shift($words));
        }
      }
      
      if (empty($controller)) {
        //$file = $base . $path . '/' . $dir . '_controller.php';
        $file = $base . $path . $dir . '_controller.php';
        
        if (! file_exists($file)) {
          throw new RouterException('Controller not found: ' . $file);
        }
        
        $controller = $dir;
      }
      
      require_once ($file);
      
      $class = Inflector::classify($controller . '_controller');
      
      if (! class_exists($class)) {
        throw new RouterException('Class not found: ' . $class);
      }
      
      /**
       * find action
       */
      
      if (count($words) && method_exists($class, 'action_' . str_replace(array('-', ' '), '_', $words[0]))) {
        $action = str_replace(array('-', ' '), '_', array_shift($words));
      }
      else {
        $action = 'index';
      }
      
      $params = count($words) ? $words : array();
      
      //$real_route = $path . '/' . $controller . '/' . $action . '/' . implode('/', $params);
      $real_route = $path . $controller . '/' . $action . '/' . implode('/', $params);
      
      $data = new Dictionary(array(
        'path' => $base . $path, 'controller' => $controller, 'action' => $action, 'params' => $params 
      ));
      
      $this->cache($route, $data);
    } else {
      $data = $this->cache[$route];
      require_once($data->get('path') . $data->get('controller') . '_controller.php');
    }
    
    return $data;
  }

}

?>